#coding: utf-8
class CulturalHeritage::CulturalHeritageFile < ActiveRecord::Base
  has_attached_file :research
  validates :title, :presence => {:message => "El Titulo es requerido"}
  validates :research_file_name, :presence => {:message => "El archivo es requerido"}
  belongs_to :obj_user, :class_name => 'User',  :foreign_key => 'user'
  belongs_to :obj_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'cultural_heritage'
  default_scope where("deleted = 0")
  validates_attachment_content_type :research, :content_type =>  [ 'application/pdf','text/plain']

  def display_name
    if !user.blank?
      return ''+title+' ('+obj_user.str_complete_name+')'
    else
      return ''+title+''
    end
  end

  def self.count_cultural_heritage_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_cultural_heritage_files.cultural_heritage = ? and (cultural_heritage_cultural_heritage_files.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_files'")[0].cultural_heritage_files.to_i
  end

  def self.count_user_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_cultural_heritage_files.user = ? and (cultural_heritage_cultural_heritage_files.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_files'")[0].cultural_heritage_files.to_i
  end
  
end